import axios from 'axios'
import { Toast } from 'vant';
let header = {
  // AppID: 'admin',
  // Version: "1.0",
  // TimeStamp: getTimeStamp(),
  // Sign: getSign(getTimeStamp())
  'Content-Type': 'application/json'

}


const request = axios.create({
  // baseURL: requestAddress.interFaceAddress,
  // baseURL: 'https://im.361sport.com/MUEAPI/',
  timeout: 10000,
  headers: header
})

// 在请求或响应被 then 或 catch 处理前拦截它们。
// 添加请求拦截器  
request.interceptors.request.use((config) => {
  let requestAddress = localStorage.getItem('requestAddress')?JSON.parse(localStorage.getItem('requestAddress')):{}
  config.baseURL = 'http://'+requestAddress.interFaceAddress
  // config.headers['TimeStamp'] = getTimeStamp()
  return config;
}, (error) => {
  // 对响应错误做点什么
  console.log('请求失败')
  return Promise.reject(error)
})

// 添加响应拦截器
request.interceptors.response.use((response) => {
  // 对响应数据做点什么
  console.log(response)
  if (response.data.status == 'error') {
    Toast(response.data.message)
  } else if (response.data.code != 0 && response.data.code) {
    Toast(response.data.message)
  }
  return response;
}, (error) => {
  console.log('请求404')
  Toast('服务器错误')
  // 对响应错误做点什么
  return Promise.reject(error);
});

export default request